from Simulinux.Views.Blocks.SourcesStepView import SourcesStepView
from Simulinux.Views.Blocks.MathGainView import MathGainView

BLOCK_VIEWS = {
    "sources.step": SourcesStepView,
    "math.gain": MathGainView
}

class CmdAddBlock:
    def __init__(self, diagram_view, diagram, refname, x, y):
        self.diagram_view = diagram_view
        self.diagram = diagram
        self.refname = refname
        self.x = x
        self.y = y

    def execute(self):
        view_class = BLOCK_VIEWS[self.refname]
        self.block = view_class.block_class(self.x, self.y)
        self.view = view_class(self.block, self.diagram_view)
        self.diagram_view.add_view(self.view)
        self.diagram.add_element(self.block)
        self.diagram_view.redraw()
    
    def reverse(self):
        self.diagram.remove_element(self.block)
        self.diagram_view.remove_view(self.view)
        self.view = None
        self.block = None
        self.diagram_view.redraw()